package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.GroupInfoRes;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.List;

/**
 * 为ExpandableListView自定义适配器
 * Created by Administrator on 2017/8/25.
 */

public class MyExpandableListView extends BaseExpandableListAdapter {

    private List<GroupInfoRes.CompanyBean> list;
    private Context context;
    private LayoutInflater inflater;

    public MyExpandableListView(List<GroupInfoRes.CompanyBean> list, Context context) {
        this.list = list;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    //返回一级列表的个数
    @Override
    public int getGroupCount() {
        return list.size();
    }

    //返回每个二级列表的个数
    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getData().size();
    }

    //返回一级列表的单个item（返回的是对象）
    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    //返回二级列表中的单个item（返回的是对象）
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getData().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    //每个item的id是否是固定？一般为true
    @Override
    public boolean hasStableIds() {
        return true;
    }

    //【重要】填充一级列表
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_group_info, null);
        }
        HXTextView company_name = (HXTextView) convertView.findViewById(R.id.company_name);
        company_name.setText(list.get(groupPosition).getName());
        return convertView;
    }

    //【重要】填充二级列表
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_group, null);
        }
        HXTextView groupnameTv = (HXTextView) convertView.findViewById(R.id.groupname_tv);
        HXTextView groupcountTv = (HXTextView) convertView.findViewById(R.id.groupcount_tv);
        GroupInfoRes.CompanyBean.TeamBean teamBean = list.get(groupPosition).getData().get(childPosition);
        groupnameTv.setText(teamBean.getName());
        groupcountTv.setText("("+teamBean.getQty()+")");
        return convertView;
    }

    //二级列表中的item是否能够被选中？可以改为true
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
